//
// Created by sgyt on 2020/5/29.
// 打印网络名称和网络编号
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>

void printNet(struct netent *pNetent);

int main(int argc, char *argv[])
{
    struct netent *net = NULL;

    //设置请求的方式
    setnetent(1);

    //获取地址
    net = getnetent();
    if(net == NULL)
    {
        perror("getnetent is error\n");
        exit(EXIT_FAILURE);
    }

    //打印输出流
    printNet(net);

    //获取地址
    net = getnetent();
    if(net == NULL)
    {
        perror("getnetent is error\n");
        exit(EXIT_FAILURE);
    }

    //打印输出流
    printNet(net);

    //关闭通道
    endnetent();
    return 0;
}

void printNet(struct netent *pNetent) {
    char **addr_al = NULL;

    printf("netent name is %s\n",pNetent->n_name);

    for(addr_al = pNetent->n_aliases;*addr_al;addr_al++)
    {
        printf("n_aliases is %s\n",*addr_al);
    }

    if(pNetent->n_addrtype == AF_INET)
    {
        printf("n_addrType is AF_INET\n");
    }else{
        printf("n_addrType is not AF_INET\n");
    }

    printf("network number: %x\n", pNetent->n_net);
}
